package com.javaweb.dao.impl;

import com.javaweb.dao.TagDao;
import com.javaweb.model.ArticleTag;
import com.javaweb.model.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.Date;
@Repository
public class TagDaoImpl implements TagDao {

    @Autowired
    private JdbcTemplate jdbcTemplate ;

//    插入标签
    @Override
    public int insert(Tag tag) {
        String sql = "INSERT INTO" +
                " tb_tag (name," +
                "create_time, update_time) VALUES (?, ?,?)" ;
        Object [] args = new Object[]{
                tag.getName(),
                // java util Date   ->   java sql Date
                new Date(tag.getCreateTime().getTime()),
                new Date(tag.getUpdateTime().getTime())
        } ;
        return jdbcTemplate.update(sql, args);
    }

//    新增文章标签
    @Override
    public int insertArticleTag(ArticleTag articleTag) {
        String sql = "INSERT INTO" +
                " tb_article_tag (article_id," +
                "tag_id) VALUES (?, ?)" ;
        Object [] args = new Object[]{
                articleTag.getArticleId(),
                articleTag.getTagId()
        } ;
        return jdbcTemplate.update(sql, args);
    }
}
